package com.meisupic.copyright.service.impl;

import com.meisupic.copyright.service.ISalesService;
import com.meisupic.copyright.support.HttpClient;
import com.meisupic.copyright.util.StringUtil;
import java.util.HashMap;
import java.util.Map;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @Author lanjian
 * @Date 2018/3/13 下午5:15
 */
@Service
public class SalesServiceImpl implements ISalesService {

  private String url = "http://www.meisupic.com/interface";

  private Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();

  @Override
  public String getUserSaleList(String userId, int page, int limit,
      String searchKey) {
    Map<String, String> params = getBaseParamsMap(userId);
    params.put("page", String.valueOf(page));
    params.put("limit", String.valueOf(limit));
    if (!StringUtil.isEmpty(searchKey)) {
      params.put("search_key", searchKey);
    }
    String path = "/get_user_sale_list.php";
    String apiUrl = url + path;
    String result = HttpClient.post(apiUrl, params).getContent();
    return result;
  }

  @Override
  public String getUserSaleInfo(String userId, String secret) {
    Map<String, String> params = getBaseParamsMap(userId);
    String path = "/get_user_sale_info.php";
    String apiUrl = url + path;
    String result = HttpClient.post(apiUrl, params).getContent();
    return result;
  }

  @Override
  public String getUserOrderList(String userId, int page, int limit,
      String searchKey, String startDate, String endDate) {
    Map<String, String> params = getBaseParamsMap( userId);
    params.put("page", String.valueOf(page));
    params.put("limit", String.valueOf(limit));
    if (!StringUtil.isEmpty(searchKey)) {
      params.put("search_key", searchKey);
    }
    if (!StringUtil.isEmpty(startDate)) {
      params.put("time_start", startDate);
    }
    if (!StringUtil.isEmpty(endDate)) {
      params.put("time_end", endDate);
    }
    String path = "/get_user_order_list.php";
    String apiUrl = url + path;
    String result = HttpClient.post(apiUrl, params).getContent();
    return result;
  }

  @Override
  public String getUserSettlementList(String userId, int page,
      int limit, String startDate, String endDate) {
    Map<String, String> params = getBaseParamsMap( userId);
    params.put("page", String.valueOf(page));
    params.put("limit", String.valueOf(limit));
    if (!StringUtil.isEmpty(startDate)) {
      params.put("time_start", startDate);
    }
    if (!StringUtil.isEmpty(endDate)) {
      params.put("time_end", endDate);
    }
    String path = "/get_user_settlement_list.php";
    String apiUrl = url + path;
    String result = HttpClient.post(apiUrl, params).getContent();
    return result;
  }

  @Override
  public String sendMessage(String tel, String content) {

    if("15954805617".equals(tel)){
      return "";
    }

    String keyId = getRandomString();
    String keyIdAndTel = keyId + tel;
    String secret = md5PasswordEncoder.encodePassword(keyIdAndTel,null);

    Map<String, String> params = new HashMap<>(16);
    params.put("key_id", keyId);
    params.put("tel", tel);
    params.put("secret", secret);
    params.put("content",content);
    String path = "/post_message.php";
    String apiUrl = url + path;
    String result = HttpClient.post(apiUrl, params).getContent();
    return result;
  }

  private Map<String, String> getBaseParamsMap(String userId) {
    String keyId = getRandomString();
    String keyIdAndUserId = keyId + userId;
    String secret = md5PasswordEncoder.encodePassword(keyIdAndUserId,null);
    Map<String, String> params = new HashMap<>(16);
    params.put("key_id", keyId);
    params.put("user_id", userId);
    params.put("secret", secret);
    return params;
  }

  private String getRandomString(){
    String KeyString = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    StringBuffer sb = new StringBuffer();
    int len = KeyString.length();
    for (int i = 0; i < 6; i++) {
      sb.append(KeyString.charAt((int) Math.round(Math.random() * (len - 1))));
    }
    return sb.toString();
  }

  public static void main(String[] args) {
    ISalesService salesService = new SalesServiceImpl();
//    String result = salesService.getUserOrderList("1058",1,10,null,null,null);
    String result = salesService.sendMessage("18979556210","测试");
    System.out.println(result);
  }


}
